package com.sh.mytool.cv.controller;

import java.io.PrintWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import com.sh.common.extjs.CUDResult;
import com.sh.common.extjs.GetResult;
import com.sh.common.file.FileUtils;
import com.sh.common.page.QueryResult;
import com.sh.common.properties.PropertiesUtils;
import com.sh.common.web.ResponseHandle;
import com.sh.mytool.cv.domain.ImageGallery;
import com.sh.mytool.cv.domain.ImageGalleryCover;
import com.sh.mytool.cv.service.ImageGalleryService;
import com.sh.common.file.FileBean;

@Controller
public class ImageGalleryController {

    @Resource(name="imageGalleryService")
    private ImageGalleryService service;

    public ImageGalleryService getService() {
        return service;
    }

    public void setService(ImageGalleryService service) {
        this.service = service;
    }

    @InitBinder
    public void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(df, true));
    }

    @RequestMapping(value = "/imagegallery/page", method = RequestMethod.POST)
    public void imageGalleryPage(@RequestParam(value="start") Integer start, @RequestParam(value="limit") Integer limit, HttpServletResponse response, PrintWriter out) throws Exception{
        QueryResult<ImageGallery> result = service.page("", null, start, limit);
        String json = ResponseHandle.handleJson("", "resultList.image", result, true);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/imagegallery/edit", method = RequestMethod.POST)
    public void imageGalleryEdit(ImageGallery imageGallery, @RequestParam(value="file") MultipartFile file, HttpServletRequest request, HttpServletResponse response, PrintWriter out) throws Exception{
        Boolean isSuccess = true;
        ImageGalleryCover cover = null;
        if(file.getSize() > 0){
            String imageroot = PropertiesUtils.getValueByResourceBundle("project", "imagecover.root");
            String path = request.getSession().getServletContext().getRealPath(imageroot);
            FileBean fileBean = FileUtils.uploadFile(file.getInputStream(), path, file.getOriginalFilename());
            cover = new ImageGalleryCover();
            cover.setName(fileBean.getName());
            cover.setPath(fileBean.getPath());
        }
        try {
            Long id = imageGallery.getId();
            if (id != null) {
                service.update(imageGallery, cover);
            } else {
                service.insert(imageGallery, cover);
            }
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setContentType("text/html;charset=UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/imagegallery/get", method = RequestMethod.POST)
    public void imageGalleryGet(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        ImageGallery result = service.get(id);
        Boolean isSuccess = false;
        if(result != null){
            isSuccess = true;
        }
        String json = ResponseHandle.handleJson("", "", new GetResult(isSuccess, result), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }

    @RequestMapping(value = "/imagegallery/remove", method = RequestMethod.POST)
    public void imageGalleryRemove(@RequestParam(value="id") Long id, HttpServletResponse response, PrintWriter out) throws Exception{
        boolean isSuccess = true;
        try {
            service.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }
        String json = ResponseHandle.handleJson("", "", new CUDResult(isSuccess), false);
        response.setCharacterEncoding("UTF-8");
        out.write(json);
        out.flush();
        out.close();
    }
}
